from datetime import date

from fastapi import APIRouter
from pydantic import BaseModel, Field
from pydantic.v1 import validator

jsonRouter=APIRouter()

class School(BaseModel):
    name: str
    level:str

class User(BaseModel):
    name: str = "surprise.lee"
    alias: str
    age:int = Field(gt=18,lt=60)
    birth:date
    schools:list[School] =[]

    @validator("alias")
    def validate_alias(self, value):
        assert value.isalpha(), 'alias must be alphanumeric'
        return value





# 输入name，level两个参数，输出一个school对象
@jsonRouter.get("/get/school",response_model=School)
async def get_school(name: str,level:str="L"):
    School.name = name
    School.level = level
    return School

# 输入User对象
@jsonRouter.post("/post")
async def post_user(user: User):
    print(user.__dict__)
    return "Success"